import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../views/Home.vue";

const routes = [{
    path: '/',
    redirect: '/dashboard'
}, {
    path: "/",
    name: "Home",
    component: Home,
    children: [{
            path: "client",
            name: "client",
            meta: {
                title: '用户管理'
            },
            component: () =>
                import ('../tags/Client.vue')
        },
        {
            path: "/pipelist",
            name: "pipelist",
            meta: {
                title: '队列管理'
            },
            component: () =>
                import ( /* webpackChunkName: "Monitor" */ "../tags/List.vue")
        },
        {
            path: "/monitor",
            name: "Monitor",
            meta: {
                title: '监控界面'
            },
            component: () =>
                import ( /* webpackChunkName: "Monitor" */ "../tags/Monitor.vue")
        },
        {
            path: "/pile",
            name: "ChargePile",
            meta: {
                title: '充电桩管理界面'
            },
            component: () =>
                import ( /* webpackChunkName: "ChargePile" */ "../tags/ChargePile.vue")
        },
        {
            path: "/dashboard",
            name: "dashboard",
            meta: {
                title: '系统首页'
            },
            component: () =>
                import ( /* webpackChunkName: "dashboard" */ "../views/Dashboard.vue")
        }, {
            path: '/404',
            name: '404',
            meta: {
                title: '找不到页面'
            },
            component: () =>
                import ( /* webpackChunkName: "404" */ '../views/404.vue')
        }, {
            path: '/403',
            name: '403',
            meta: {
                title: '没有权限'
            },
            component: () =>
                import ( /* webpackChunkName: "403" */ '../views/403.vue')
        },{
                path: "/setting",
                name: "Setting",
                meta: {
                    title: '设置页面'
                },
                component: () => import ( /* webpackChunkName: "ChargePile" */ "../tags/Setting.vue")
            },
    ]
}, {
    path: "/login",
    name: "Login",
    meta: {
        title: '登录'
    },
    component: () =>
        import ( /* webpackChunkName: "login" */ "../tags/Login.vue")
}];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

// router.beforeEach((to, from, next) => {
//     document.title = `${to.meta.title} | vue-manage-system`;
//     const role = localStorage.getItem('ms_username');
//     if (!role && to.path !== '/login') {
//         next('/login');
//     } else if (to.meta.permission) {
//         // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
//         role === 'admin'
//             ? next()
//             : next('/403');
//     } else {
//         next();
//     }
// });
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
        // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router;